package com.gywj.race.service;

import com.alibaba.fastjson.JSON;
import com.aliyun.oss.ClientException;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SendSmsService {
    @Value("${spring.application.ali.sms.regionId}")
    private String regionId;

    @Value("${spring.application.ali.sms.signName}")
    private String signName;


    @Value("${spring.application.ali.sms.templateCode}")
    private String templateCode;


    @Value("${spring.application.ali.sms.accessKeyId}")
    private String accessKeyId;

    @Value("${spring.application.ali.sms.accessKeySecret}")
    private String accessKeySecret;


    public void sendSms(String mobile, Map contentJsonString) {
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        if (!"{}".equals(JSON.toJSONString(contentJsonString))) {
            request.putQueryParameter("TemplateParam", JSON.toJSONString(contentJsonString));
        }

        try {
            CommonResponse response = null;
            response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (com.aliyuncs.exceptions.ClientException e) {
            e.printStackTrace();
        }
    }
}
